package com.liehu.business.controller;


import com.liehu.business.pojo.Type;
import com.liehu.business.service.TypeService;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/type")
@CrossOrigin //跨域处理
@Api(value = "/type",description = "文章类型")
public class TypeController {
    @Autowired
    private TypeService typeService;

    /**
     * 查询所有
     */
    @ApiOperation(value = "查询全部类型", notes = "查询全部类型")//描述
    @GetMapping()
    public Result findAll(){
        return  new Result(true, StatusCode.OK,"查询成功！",typeService.findAll());
    }
    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询", notes = "根据id查询")//描述
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id){
        return  new Result(true, StatusCode.OK,"查询成功！",typeService.findById(id));
    }
    /**
     * 插入一条数据
     */
    @ApiOperation(value = "插入一条数据", notes = "插入一条数据")//描述
    @PostMapping("save")
    public Result save(@RequestBody Type type){
        typeService.save(type);
        return  new Result(true, StatusCode.OK,"新增成功！");
    }
    /**
     * 根据id修改记录
     * @param type
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id修改记录", notes = "根据id修改记录")//描述
    @PutMapping("/{id}")
    public Result update(@RequestBody Type type,@PathVariable("id") String id){
        type.setId(id);//植入id
        typeService.update(type);
        return  new Result(true, StatusCode.OK,"修改成功！");
    }
    /**
     * 根据id删除
     */
    @ApiOperation(value = "根据id删除", notes = "根据id删除")//描述
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        typeService.deleteById(id);
        return  new Result(true, StatusCode.OK,"删除成功！");
    }
}
